# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Michael Zhou <zhoumichaely@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Daniel Sabogal <dsabogalcc@gmail.com>
# Maintainer: Sören Tempel <soeren+alpine@soeren-tempel.net>
pkgname=mupdf
# XXX: even non-breaking changes require rebuilds against revdeps,
# upstream does not provide ABI stability and no versioned .so files.
pkgver=1.24.10
pkgrel=0
pkgdesc="Lightweight PDF and XPS viewer"
url="https://mupdf.com"
arch="all"
license="AGPL-3.0-or-later"
makedepends="
	curl-dev
	desktop-file-utils
	freetype-dev
	glu-dev
	gumbo-parser-dev
	harfbuzz-dev
	jbig2dec-dev
	jpeg-dev
	libx11-dev
	libxext-dev
	libxi-dev
	libxrandr-dev
	mesa-dev
	mujs-dev
	openjpeg-dev
	readline-dev
	zlib-dev
	"
subpackages="
	$pkgname-doc
	$pkgname-dev
	$pkgname-libs
	$pkgname-x11:_x11
	$pkgname-gl:_gl
	$pkgname-tools:_tools
	"
source="$pkgname-$pkgver.tar.gz::https://mupdf.com/downloads/archive/mupdf-$pkgver-source.tar.gz
	respect-cflags.patch

	mupdf.pc.in
	mupdf.desktop
	mupdf
	"
builddir="$srcdir/$pkgname-$pkgver-source"
options="!check"

# secfixes:
#   1.18.0-r1:
#     - CVE-2021-3407
#   1.17.0-r3:
#     - CVE-2020-26519
#   1.13-r0:
#     - CVE-2018-1000051
#     - CVE-2018-6544
#     - CVE-2018-6192
#     - CVE-2018-6187
#     - CVE-2018-5686
#     - CVE-2017-17858
#   1.11-r1:
#     - CVE-2017-6060
#   1.10a-r2:
#     - CVE-2017-5991
#   1.10a-r1:
#     - CVE-2017-5896

# Needs freeglut2-art fork.
_make_flags="
	build=release
	shared=yes
	USE_SYSTEM_LIBS=yes
	USE_SYSTEM_MUJS=yes
	USE_SYSTEM_GLUT=no
	"

prepare() {
	default_prepare

	local dir; for dir in thirdparty/*; do
		case "${dir##*/}" in
		extract|freeglut|lcms2) continue ;;
		*) rm -rf $dir ;;
		esac
	done

	sed "s/@@VERSION@@/$pkgver/" "$srcdir"/$pkgname.pc.in > $pkgname.pc
}

build() {
	make $_make_flags verbose=yes libs apps
}

package() {
	depends="mupdf-viewer"

	make $_make_flags prefix=/usr DESTDIR="$pkgdir" install
	ln -s libmupdf.so.${pkgver#*.} "$pkgdir"/usr/lib/libmupdf.so

	install -D -m644 $pkgname.pc -t "$pkgdir"/usr/lib/pkgconfig/
	install -D -m755 "$srcdir"/mupdf -t "$pkgdir"/usr/bin/
	install -D -m644 docs/logo/mupdf-logo.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/$pkgname.svg
	desktop-file-install --dir="$pkgdir/usr/share/applications" "$srcdir"/$pkgname.desktop
}

libs() {
	replaces="$pkgname<1.20.0-r3"  # backward compatibility (Alpine <3.17)
	default_libs
}

_x11() {
	pkgdesc="$pkgdesc with X11 backend"
	depends=""
	# NOTE: Not specifying version to allow installing -x11 and -gl in parallel.
	provides="$pkgname-viewer"
	provider_priority=10  # lowest

	amove usr/bin/mupdf-x11
	amove usr/bin/mupdf-x11-curl
}

_gl() {
	pkgdesc="$pkgdesc with OpenGL backend"
	depends=""
	provides="$pkgname-viewer"
	provider_priority=100  # highest

	amove usr/bin/mupdf-gl
}

_tools() {
	pkgdesc="Tools for a lightweight PDF and XPS viewer"
	depends=""

	amove usr/bin/mutool
	amove usr/bin/muraster
}

sha512sums="
4f2d1447e6d090d461af761df1a15bf054fe56d3e8f6a8a01bedd103ae6bdefb98e98d76af93d34e8aa61ae06b94eca15dcddfc4411c5288d1c77d1ce31563a8  mupdf-1.24.10.tar.gz
0158ce75ca6f3195ba31455803478235bea145c2442593cde4cf982ea0faa3cc497d7ab39bebce725cb07ef35266034dbafe4c16f44598db746769608eac9bd8  respect-cflags.patch
3135f8736fd9e7127a860967c83731af417f96133f8d0fd8ba7ae65985445dc095b84dc91d4a612eae017aefdc60608ca9e1f69867249f9ffa6896e996749d63  mupdf.pc.in
13eb39b20bf1c2bba4af010336aaa168ac9a643e7c78dda5de0d908174f930706ba22fee1c2a2a2d245487027b125abe268ae74810a82a3c8aa182d3f1485f9c  mupdf.desktop
affbb393536b5887ad53f027087551b0047b2e4e6f3fc1397a362ca7425a230e40492f76c929980c2e27e46317fa76575c3dc6f9672f7061fb15140d8965b128  mupdf
"
